package com.yx.study.leet.questionList.questions;

import org.junit.Test;

/**
 * 27. 移除元素
 * https://leetcode.cn/problems/remove-element/
 * */
public class RemoveElement27 {
    public int removeElement(int[] nums, int val) {
        int left = 0;
        int right = nums.length;
        while (left < right) {
            if (nums[left] == val) {
                nums[left] = nums[right - 1]; // 将右指针的值赋值给左指针
                right--;
            } else {
                left++;
            }
        }
        return left;
    }

    @Test
    public void test(){
        // [3,2,2,3]
        int[] arr = new int[]{3,2,2,3};
        System.out.println(removeElement(arr, 3));
    }
}
